<?php
namespace app\index\controller;

use think\Controller;
// use think\Request;
use think\facade\Request;
use think\Config;

use app\index\model\RedisModel;
use app\index\util\Response;

class Base extends Controller
{
    public $header;
    public $request;
    public $user;
    public $baseData;

    //不需要登录的操作
    private $noLogin = [
        'Alipay' => [
            'returnurl',
        ],
        'User' => [
            '*',
        ],
        
    ];
    
    public function __construct()
    {
        parent::__construct();
        
        // $this->header = Request::header();
        // $this->request = Request::instance();
        // $this->request = response()->data();
        $user = $this->checkToken();
        $this->response = new Response();
        // $this->baseData = $this->getBaseData();
        $this->user = $user;
        if ($user === false) {
            header("Content-type:application/json");
            //登录验证，待优化
            echo json_encode([
                'code' => 600,
                'message' => '登录过期，请重新登录',
                'data' => new \stdClass(),
                'StackTrace' => '',
            ]);
            exit();
        }
    }

    /**
     * Undocumented function
     *
     * @param string $str 字符串
     * 
     * @return void
     */
    public function param($str)
    {
        $data = Request::param($str); 
        if (isset($data)) {
            return $data;
        } 
        return null;
    }
    
    public function checkToken()
    {
        $controllerName = $this->request->controller();
        $actionName = $this->request->action();
        $info = null;
        $redisModel = new RedisModel();
        if (isset($this->header['token'])) {
            $token = $this->header['token'];
            $info = $redisModel->redisGet($token);
        }
        //判断是否传入token，不传入则验证是否需要传入
        if (!isset($info) or $info === null or $info === false) {
            
            if (isset($this->noLogin[$controllerName])) {
                if ((in_array($actionName, $this->noLogin[$controllerName])) || $this->noLogin[$controllerName][0] == '*') {
                    return true;
                }
            }
            return false;
        }
        $info = json_decode($info, true);
        if (time() - $info['validtime'] > Config::get('time_out')) {
            $redisModel->redisDel($token);
            return false;
        }
        if (!isset($info['id'])) {
            echo json_encode([
                'code' => 600,
                'message' => '用户信息有误，请重新登录',
                'data' => new \stdClass(),
                'StackTrace' => '',
            ]);
            exit();
        }
        $info['validtime'] = time();
        $redisVal = json_encode($info);
        if ($redisModel->redisSet($token, $redisVal, Config::get('time_out')) === false) {
            // $this->redisModel->rollback();
            return false;
        }
        $userModel = new userModel();
        return $userModel->userInfo($info['id']);
    }

}
